package com.irdstudio.efp.esb.service.client.impl;

import com.baidu.disconf.client.common.annotations.DisconfItem;
import com.irdstudio.basic.framework.core.util.SpringContextUtils;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.basic.sequence.service.DefaultPatternDescriptor;
import com.irdstudio.basic.sequence.service.PatternDescriptor;
import com.irdstudio.basic.sequence.service.facade.PatternedLimitableSeqService;
import com.irdstudio.efp.esb.common.client.req.EsbReqAppHeadBean;
import com.irdstudio.efp.esb.common.client.req.EsbReqSysHeadBean;
import com.irdstudio.efp.esb.common.constant.ESBClientConstance;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.*;

@Component("BD3022001101ESBBeanCreator")
public class BD3022001101ESBBeanCreator extends DefaultESBBeanCreator {

	@Value("${esb.client.bd.usrno}")
	private String usrno;
	
	@PostConstruct
	public void test () {
		System.out.println(usrno);
	}

	@DisconfItem(key = "esb.client.bd.usrno", associateField = "usrno")
	public String getUsrno() {
		return usrno;
	}

	public void setUsrno(String usrno) {
		this.usrno = usrno;
	}

	@Override
	protected EsbReqAppHeadBean buildAppHead() {
		EsbReqAppHeadBean appHead = new EsbReqAppHeadBean();
		appHead.setInstId(ESBClientConstance.InstId);
		appHead.setUsrNo(usrno);
		appHead.setCnlTp(ESBClientConstance.CnlTp);

		appHead.setUsrPswd("");
		appHead.setUsrLvl("");
		appHead.setUsrTp("");
		appHead.setTlrSrlNo("");
		appHead.setCnlDtlTp("");
		appHead.setCnsmrTxnCd("");
		return appHead;
	}
	private static DateTimeFormatter dateForm = DateTimeFormatter.ofPattern("yyyyMMdd");
	private static DateTimeFormatter timeForm = DateTimeFormatter.ofPattern("HHmmss");

	@Override
	protected EsbReqSysHeadBean buildSysHead(String serverCode, String scene) throws Exception {
		EsbReqSysHeadBean syshead = new EsbReqSysHeadBean();
		syshead.setSvcCd(serverCode); // 要调用的服务代码
		syshead.setScnCd(scene); // 服务的应用场景
		syshead.setCnsmrSysID(ESBClientConstance.CnsmrSysNo); // 网贷的系统id
		syshead.setOrgnlCnsmrSysID(ESBClientConstance.CnsmrSysNo); // 网贷的系统id
		String globalSrlNo = TraceUtil.getTraceId();
		String consumerSrlNo = generateESBTradeSerialNo();
		syshead.setGlblSrlNo(globalSrlNo == null ? consumerSrlNo : globalSrlNo);
		syshead.setCnsmrSrlNo(consumerSrlNo);
		LocalDateTime date = LocalDateTime.now();
		syshead.setTxnDt(dateForm.format(date));
		syshead.setTxnTm(timeForm.format(date));
		syshead.setTxnMd(ESBClientConstance.TxnMd.ONLINE.name());
		syshead.setUsrLng(ESBClientConstance.UsrLng.CHINESE.name());
		syshead.setFileFlg(ESBClientConstance.FileFlg.NONE.VALUE);
		syshead.setEryptMd(ESBClientConstance.EryptMd.NONE.VALUE);
		syshead.setPlafmID(TraceUtil.getPlafmID());
		syshead.setPlafmUsrID(TraceUtil.getPlafmUsrID());
		syshead.setCompany(ESBClientConstance.Company);
		syshead.setOrgnlTmlIdNo("");
		syshead.setTmlIdNo("");
		syshead.setOrgnlCnsmrSvcNo("");
		syshead.setCnsmrSvcNo("");
		syshead.setMACVal("");
		syshead.setMACFctr("");
		syshead.setPINSd("");
		syshead.setSvcVerNo("");
		syshead.setVerfFlg("");
		syshead.setMsgTp("");
		syshead.setPltfmClntNo("");
		syshead.setPrdctID("");
		syshead.setRelGlblSrlNo("");
		return syshead;
	}
	/* 生成ESB系统的交易流水号(CnsmrSrlNo) */
	private String generateESBTradeSerialNo() throws Exception {
		PatternedLimitableSeqService sequenceService = SpringContextUtils.getBean(PatternedLimitableSeqService.class);
		return sequenceService.getPatternedSequence("ESB", pattern);
	}
	private static final PatternDescriptor pattern =
			DefaultPatternDescriptor.of(ESBClientConstance.CnsmrSysNo +
					YEAR_2 + MONTH + DAY + HOUR + MIN + SECOND + SEQ, 6);
}
